<?php

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
 	require_once (APPLICATION_PATH . "\\models\\connectionDb.php");
} else {
    require_once (APPLICATION_PATH . "//models//connectionDb.php");
}

class Application_Model_DbTable_Medicalinfothreatmenthistory extends Zend_Db_Table_Abstract
{

    protected $_name = 'MedicalInfo_ThreatmentHistory';
    protected $_primary = 'id';
    
    public function getCurrentThreatment($medicaInfo_id)
    {
    	try{
    		
    		$sql ="select MedicalInfo_ThreatmentHistory.*,ThreatmentsHistory.Name,ThreatmentsHistory.Description";
			$sql .=" from MedicalInfo_ThreatmentHistory";
			$sql .=" left join ThreatmentsHistory on ThreatmentsHistory.id = MedicalInfo_ThreatmentHistory.ThreatmentHistory";
			$sql .=" where 1=1";
			$sql .=" and MedicalInfo_ThreatmentHistory.DateStart < CURDATE()";
			$sql .=" and MedicalInfo_ThreatmentHistory.DateEnd > CURDATE()";
			$sql .=" and MedicalInfo_ThreatmentHistory.MedicalInfo_id=".$medicaInfo_id;
    		 
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    }
	public function getAllThreatmentsNonActive($medicaInfo_id)
    {
    	try{
    		
    		$sql ="select ThreatmentsHistory.id,ThreatmentsHistory.Name";
			$sql .=" from ThreatmentsHistory";
			$sql .=" where ThreatmentsHistory.id NOT IN (select MedicalInfo_ThreatmentHistory.ThreatmentHistory";
			$sql .=" from MedicalInfo_ThreatmentHistory";			
			$sql .=" where 1=1";  
			$sql .=" and MedicalInfo_ThreatmentHistory.DateStart < CURDATE()";  		 
			$sql .=" and MedicalInfo_ThreatmentHistory.DateEnd > CURDATE()";
			$sql .=" and MedicalInfo_ThreatmentHistory.MedicalInfo_id=".$medicaInfo_id.")";
    		$db = connectionDb::connectDB();		
			$rows = $db->fetchAll($sql);			
	  					 
	    	if (!$rows) {
	   			return null;
	  		}
	  		return $rows;
    	
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        }
    
    }
	public function AddThreatmentForUser($medicalInfo,$threatment, $dateFrom,$dateTo)
    {
    	$data = array(
    		'MedicalInfo_id' => $medicalInfo,
    		'ThreatmentHistory' => $threatment,
    		'DateStart' => $dateFrom,
    		'DateEnd' => $dateTo,
    	);
    	$this->insert($data);
    }
    
	public function deleteThreatmentForUser($id)
    {	
    	try {
    		$this->delete('id = ' . (int)$id);
    	}
    	catch (Zend_Exception $e) {  
			echo "Error message: " . $e->getMessage() . "\n";
        } 
    	
    }


}

